<?php
namespace App\Domain;
use App\Model\Charge as Model_Charge;

class Charge{

    /* 充值规则 */
	public function getChargeRules($uid='') {
        
        $key='getChargeRules';
		$rules=\App\getcaches($key);
		\App\delcache($key);
		if(!$rules){
            $model = new Model_Charge();
			$rules= $model->getChargeRules($uid);
			\App\setcaches($key,$rules);
		}
		
		/*$lang=\PhalApi\DI()->lang;
		foreach($rules as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$rules[$k]=$v;
		}*/

		return $rules;
	}

    /* 生成订单 */
	public function setOrder($changeid,$orderinfo) {
		$rs = array('code' => 0, 'msg' => '', 'info' => array());
        $user = \App\getUserInfo($orderinfo['uid']);
		$country = $user['country'];
		$model = new Model_Charge();
        $charge = $model->getChargeRule($changeid);
        if(!$charge){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;
        }
        if(!empty($charge['money'])){
            $money = json_decode($charge['money'],true);
            $charge['money'] = '';
            foreach ($money as $k=>$v){
                if($k == $country['code']){
                    $charge['money'] = $v;
                }
            }
            if(empty($charge['money'])){
                foreach ($money as $k=>$v){
                    if($k == 'US'){
                        $charge['money'] = $v;
                    }
                }
            }
        }else{
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;
        }
        if($charge['money']!=$orderinfo['money'] || ($charge['coin']!=$orderinfo['coin']  && $orderinfo['coin']!=$charge['coin_ios']   && $orderinfo['coin']!=$charge['coin_paypal'] )){
			$rs['code']=1003;
			$rs['msg']=\PhalApi\T('信息错误');
			return $rs;
		}
		
		$orderinfo['coin_give']=$charge['give'];
        
		$rs = $model->setOrder($orderinfo);

		return $rs;
	}
	
	public function updatePaymentStatus($uid,$nonce,$orderid,$money) {
		$rs = array();

		$model = new Model_Charge();
		
		$rs = $model->updatePaymentStatus($uid,$nonce,$orderid,$money);

		return $rs;
	}
	
}
